using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Ent.Web.Helper;
using System.Web.UI.WebControls;
namespace Ent.Web
{
    public class Settings
    {
        XMLHelper _xml;
        public Settings(string path)
        {
            _xml = new XMLHelper(path);
        }
        public ListItem[] GetItemCollection(string CollectionName, ListItem[] additionalParam)
        {
            ListItem[] main = GetItemCollection(CollectionName);
            ListItem[] retValue = new ListItem[main.Length + additionalParam.Length];
            for (int i = 0; i < main.Length + additionalParam.Length; i++)
            {
                if (i < additionalParam.Length)
                    retValue.SetValue(additionalParam.GetValue(i), i);
                else
                    retValue.SetValue(main.GetValue(i - additionalParam.Length), i);
            }
            return retValue;
        }
        public ListItem[] GetItemCollection(string CollectionName)
        {
            try
            {
                ListItem[] retValue = new ListItem[0];
                //Item[] retValue = null;
                DataTable dt = _xml.GetChildNodesAsDataTable("//Settings/Collection[@name='" + CollectionName + "']", "Item", new string[] { "id", "name", "selected" });
                if (dt != null)
                {
                    retValue = new ListItem[dt.Rows.Count];
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListItem item = new ListItem();
                        item.Value = dt.Rows[i]["id"].ToString();
                        item.Text = dt.Rows[i]["name"].ToString();
                        item.Selected = MathFunction.ConvertToBoolean(dt.Rows[i]["selected"]);
                        retValue.SetValue(item, i);
                    }
                }
                return retValue;
            }
            catch (Exception exc)
            {
                return null;
            }
        }
        
    }
}
